package com.ruoyi.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.ams.domain.vo.AssetProjectAddVo;
import com.ruoyi.ams.domain.vo.AssetProjectEditVo;
import com.ruoyi.ams.domain.vo.AssetProjectListVo;
import com.ruoyi.ams.domain.vo.KeyValueVo;
import com.ruoyi.ams.service.IAmsAssetProjectService;
import com.ruoyi.common.core.domain.AsmsAssetFullInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.AmsAssetProject;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 资产批次信息Controller
 *
 * @author yfkj
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/ams/AmsAssetProject")
public class AmsAssetProjectController extends BaseController {
    @Autowired
    private IAmsAssetProjectService amsAssetProjectService;

   // @Autowired
 //   private IAsmsAssetFullInfoService assetFullInfoService;
    @Autowired
    private com.ruoyi.asms.service.IAsmsAssetFullInfoService assetFullInfoService;
    /**
     * 查询资产批次信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetProject:list")
    @GetMapping("/list")
    @ApiOperation("查询资产批次信息列表")
    public TableDataInfo list(AssetProjectListVo vo) {
        startPage();
        AmsAssetProject amsAssetProject = new AmsAssetProject();
        BeanUtils.copyProperties(vo, amsAssetProject);
        List<AmsAssetProject> list = amsAssetProjectService.selectAmsAssetProjectList(amsAssetProject);
        return getDataTable(list);
    }

    /**
     * 下拉选择列表
     */
    @ApiOperation("品牌下拉列表")
    @GetMapping("/selectList")
    public AjaxResult list() {
        List<AmsAssetProject> list = amsAssetProjectService.list(new QueryWrapper<AmsAssetProject>());
        List<KeyValueVo> vos = new ArrayList<>();
        for (AmsAssetProject project : list) {
            KeyValueVo vo = new KeyValueVo();
            vo.setKey(project.getProjectId());
            vo.setValue(project.getProjectName());
            vos.add(vo);
        }
        return AjaxResult.success(vos);
    }

    /**
     * 导出资产批次信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetProject:export")
    @Log(title = "资产批次信息", businessType = BusinessType.EXPORT)
    @PostMapping(value="/export", produces = "application/octet-stream")
    @ApiOperation("导出资产批次信息列表")
    public void export(HttpServletResponse response, AmsAssetProject amsAssetProject) throws IOException {
        List<AmsAssetProject> list = amsAssetProjectService.selectAmsAssetProjectList(amsAssetProject);
        ExcelUtil<AmsAssetProject> util = new ExcelUtil<AmsAssetProject>(AmsAssetProject.class);
        util.exportExcel(response, list, "资产批次信息数据");
    }

    /**
     * 获取资产批次信息详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetProject:query")
    @GetMapping(value = "/{projectId}")
    @ApiOperation("获取资产批次信息详细信息,projectId-资产批次信息ID-Long-必填")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId) {
        return AjaxResult.success(amsAssetProjectService.selectAmsAssetProjectByProjectId(projectId));
    }

    /**
     * 新增资产批次信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetProject:add")
    @Log(title = "资产批次信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增资产批次信息")
    public AjaxResult add(@RequestBody AssetProjectAddVo vo) {
        AmsAssetProject amsAssetProject = new AmsAssetProject();
        BeanUtils.copyProperties(vo, amsAssetProject);
        AmsAssetProject project = amsAssetProjectService.getOne(new QueryWrapper<AmsAssetProject>()
                .eq(AmsAssetProject.PROJECT_NAME, vo.getProjectName()), false);
        if (project != null) {
            return AjaxResult.error(vo.getProjectName() + "已存在！");
        }
        amsAssetProject.setCreateBy(SecurityUtils.getUsername());
        amsAssetProject.setCreateTime(System.currentTimeMillis());
        return toAjax(amsAssetProjectService.insertAmsAssetProject(amsAssetProject));
    }

    /**
     * 修改资产批次信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetProject:edit")
    @Log(title = "资产批次信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改资产批次信息")
    public AjaxResult edit(@RequestBody AssetProjectEditVo vo) {
        AmsAssetProject amsAssetProject = new AmsAssetProject();
        BeanUtils.copyProperties(vo, amsAssetProject);
        amsAssetProject.setUpdateBy(SecurityUtils.getUsername());
        amsAssetProject.setUpdateTime(System.currentTimeMillis());

        //修改资产视图表中的相关数据
        assetFullInfoService.update(new UpdateWrapper<AsmsAssetFullInfo>()
                .set(AsmsAssetFullInfo.PROJECT_NAME,vo.getProjectName())
                .set(AsmsAssetFullInfo.PROJECT_DESCRIPTION,vo.getProjectDescription())
                .eq(AsmsAssetFullInfo.PROJECT_ID,vo.getProjectId()));
        return toAjax(amsAssetProjectService.updateAmsAssetProject(amsAssetProject));
    }

    /**
     * 删除资产批次信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetProject:remove")
    @Log(title = "资产批次信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectId}")
    @ApiOperation("删除资产批次信息,projectId-资产批次ID-Long-必填")
    public AjaxResult remove(@PathVariable Long projectId) {
        return toAjax(amsAssetProjectService.deleteAmsAssetProjectByProjectId(projectId));
    }

    @GetMapping("/getProjectList")
    public R<List<AmsAssetProject>> getProjectList() {
        return R.ok(amsAssetProjectService.list());
    }
}
